package com.bawei.database

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.bawei.database.db.AppDataBase

/**
 * Author: Jason Zhang
 * Date: 2025/4/25
 * Desc: 数据库工具
 */
object RoomDBUtils {

    private lateinit var dataBase: AppDataBase

    //手动数据库迁移
    private val MIGRATION_1_2 = object : Migration(1, 2) {
        override fun migrate(database: SupportSQLiteDatabase) {
            //修改数据库表名
            val renameTableSql = "ALTER TABLE user_info_table RENAME TO user_info"
            database.execSQL(renameTableSql)
        }
    }

    /**
     * 初始化数据库
     */
    fun initRoomDB(context: Context){
        dataBase = Room.databaseBuilder(context, AppDataBase::class.java, "park.db")
//            .addMigrations(MIGRATION_1_2)
            .build()
    }


    /**
     * 获取数据库实例
     */
    fun getDataBase(): AppDataBase {
        if (dataBase == null){
            throw RuntimeException("DataBase not initialized")
        }
        return dataBase
    }


}